package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.common.TokenUtils;
import com.example.demo.entity.Admin;
import com.example.demo.entity.User;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    IUserService userService;



    /**
     * 保存用户
     *
     * @param user 用户信息
     * @return Result
     */
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        return userService.save(user) ? Result.success() : Result.error("保存失败");
    }

    /**
     * 更新用户
     *
     * @param user 用户信息
     * @return Result
     */
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        return userService.updateById(user) ? Result.success() : Result.error("更新失败");
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return Result
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return userService.removeById(id) ? Result.success() : Result.error("删除失败");
    }

    /**
     * 根据id查询用户
     *
     * @param id 用户id
     * @return Result
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 查询所有用户
     *
     * @return Result
     */
    @GetMapping("/list")
    public Result list() {
        List<User> users = userService.list(new QueryWrapper<User>().last("limit 150"));
        return Result.success(users);
    }

    /**
     * 分页查询用户
     *
     * @param
     * @return Result
     */
    @GetMapping("/page")
    public Result search(@RequestParam(required = false) Integer studentId,
                         @RequestParam(required = false) String name,
                         @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                         @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        IPage<User> page = new Page<>(pageNum, pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (studentId != null) {
            wrapper.eq("studentId", studentId);
        }
        if (name != null) {
            wrapper.like("name", name);
        }
        IPage<User> adminIPage = userService.page(page, wrapper);
        return Result.success(adminIPage);
    }
    //根据学号模糊搜索
    @GetMapping("/search")
    public Result search(@RequestParam(required = false) Integer studentId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (studentId != null) {
            wrapper.eq("studentId", studentId);
        }
        List<User> users = userService.list(wrapper);
        return Result.success(users);
    }
    //TODO
    //用户登录
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        return userService.login(user);
    }
    //TODO
    //修改密码
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("studentId", user.getStudentId());
        wrapper.eq("password", user.getPassword());
        User user1 = userService.getOne(wrapper);
        if (user1 != null) {
            user1.setPassword(user.getNewPassword());
            return userService.updateById(user1) ? Result.success() : Result.error("修改失败");
        } else {
            return Result.error("用户名或密码错误");
        }
    }
    //退出
    @GetMapping("/logout")
    public Result logout() {
        //清除token
        TokenUtils.clearToken();
        return Result.success();
    }
}
